package thread;

public class demo11 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("hello thread!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //当触发异常之后 立刻退出循环
                    break;
                }
            }
        });
        t.start();

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        /**调用这个方法可能产生两种1情况
         * 1、如果t线程处在就绪状态，就是设置线程的标志位为true
         * 2、如果t线程处在阻塞状态，就会触发一个InterruptException
         */
        t.interrupt();
    }
}
